# coding=utf-8
# Django认证的父类
import re
from django.contrib.auth.backends import ModelBackend
from .models import Users

class MutiAccountLoginAuth(ModelBackend):
    # 重写方法
    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            # 用正则表达式的方法来匹配用户名，如果匹配的话就是手机号登录
            if re.match('^1[35789]\d{9}$',username):
                user = Users.objects.get(phone=username)
            else: # 用户名登录
                user = Users.objects.get(username=username)
        except Users.DoesNotExist: # 查询不到用户
            user = None

        # 如果user有值而且密码也校验正确的话，就把user返回出去
        if user and user.check_password(password):
            return user



